import util from "../../util";
import parentCollection from '../../others/parent/parentCollection';

export default class ApiConfigCollection extends parentCollection{
    constructor(path) {
        super(ApiConfigCollection, util.api.ApiConfig);
    }
    static create(record, path){
        let object=new ApiConfigCollection(path);
        return object;
    }
    /**
     * 条件查询，当前版本，不分页
     * */
    async query(filter){
        let result = await this.api.query({condition:filter}).then(data=>{
            if(!!data) {
                this.records = data.sort((a,b) => `${a.category}_${a.name}`.localeCompare(`${b.category}_${b.name}`, 'zh-Hans-CN'));
                this.recordsCascader = util.getCascader(data, 'category', 'name')
                let recordsMap = {}
                data.forEach(item => {
                    recordsMap[`${item.category}_${item.name}`] = item
                });
                this.recordsMap = recordsMap
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async queryInfo(filter){
        let result = await this.api.queryInfo({condition:filter}).then(data=>{
            if(!!data) {
                this.records = data.sort((a,b) => `${a.category}_${a.name}`.localeCompare(`${b.category}_${b.name}`, 'zh-Hans-CN'));
                this.recordsCascader = util.getCascader(data, 'category', 'name')
                let recordsMap = {}
                data.forEach(item => {
                    recordsMap[`${item.category}_${item.name}`] = item
                });
                this.recordsMap = recordsMap
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
}
